package org.luckyjourney.service.plan.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.luckyjourney.entity.plan.dto.PlanDTO;
import org.luckyjourney.entity.plan.vo.PlanVO;
import org.luckyjourney.entity.plan.vo.PlanVOS;
import org.luckyjourney.exception.BaseException;
import org.luckyjourney.mapper.plan.ConsultantsPlanMapper;
import org.luckyjourney.service.plan.ConsultantsPlanService;
import org.luckyjourney.util.BaseContext;
import org.luckyjourney.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class ConsultantsPlanServiceImpl implements ConsultantsPlanService {
    @Autowired
    ConsultantsPlanMapper consultantsPlanMapper;

    @Override
    public List<PlanVOS> selectPage(PlanDTO planDTO) {
        Long userId = planDTO.getUserId();
        if (ObjectUtil.isEmpty(userId)) {
            throw new BaseException("请选择要查看的咨询");
        }
        planDTO.setUserId(userId);

        // 创建一个SimpleDateFormat对象，用于格式化时间
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前以后的时间
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 获取系统当前时间
        LocalDate date = LocalDate.now().minusDays(1);
        // String format = sdf.format(date);
        String format = date.format(fmt);
        planDTO.setCreatebeginTime(format);
        List<PlanVOS> planVOS = consultantsPlanMapper.selectPage(planDTO);
        ArrayList<PlanVOS> list = new ArrayList<>();

        for (PlanVOS planVO : planVOS) {
            //返回排班的日期跟星期
            PlanVOS vos = new PlanVOS();
            String planDate = planVO.getPlanDate();
            long whatDaySomeDateMillis = getWhatDaySomeDateMillis(planDate);
            Date toDayDate = new Date(whatDaySomeDateMillis);
            SimpleDateFormat formatE = new SimpleDateFormat("E",Locale.CHINA);
            String week = null;
            try {
                week = formatE.format(toDayDate);
                System.out.println("星期" + week);
            } catch (Exception e) {
                e.printStackTrace();
            }
            vos.setPlanDate(planDate);
            vos.setWeek(week);
            list.add(vos);
        }
        return list;
    }

    // someDataStr 格式："yyyy-MM-dd"
    // 返回 时间毫秒
    private static long getWhatDaySomeDateMillis(String someDataStr) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(someDataStr);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1;
    }
}
